package com.zyx.service;

/**
  * @author zyx
  * @date 2022/1/12
  */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyx.mapper.RelevancyMapper;
import com.zyx.pojo.Name;
import com.zyx.pojo.Relevancy;
import com.zyx.pojo.RelevancyExample;
import com.zyx.pojo.Spencer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class RelevancyService {
    @Resource
    private RelevancyMapper relevancyMapper;

    @Resource
    private AutoService autoService;

    /**
     * 查询所有店铺
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Relevancy> selectByName(){
        return relevancyMapper.selectBySpencer();
    }

    /**
     * 查询店铺车辆数量
     * @param spencerId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public int queryByCount(int spencerId){
        return relevancyMapper.queryByCount(spencerId);
    }

    /**
     * 查询店铺车辆类型
     * @param spencerId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Relevancy> queryByType(int spencerId){
        List<Relevancy> list = relevancyMapper.queryByType(spencerId);
        for (Relevancy r : list){
            Name n = r.getName();
            n.setPriceMax(autoService.queryMax(n.getCarName()));
            n.setPriceMin(autoService.queryMin(n.getCarName()));
            r.setName(n);
        }
        return list;
    }

    /**
     * 根据车型id和车辆id查询店铺
     * @author zyx
     * @date 22:16 2022/4/26
     * @name querySpencer
     * @params [autoId, carId]
     * @return java.util.List<com.zyx.pojo.Spencer>
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Spencer> querySpencer(int autoId,int carId){
        return relevancyMapper.querySpencer(autoId,carId);
    }

    /**
     * 查询店铺某个车辆类型的车辆型号
     * @param spencerId
     * @param carId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Relevancy> queryByAuto(int spencerId,int carId){
        List<Relevancy> list = relevancyMapper.queryByAuto(spencerId,carId);
        return list;
    }

    /**
     * 根据店铺Id查询所有车辆名称
     * @param i
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Relevancy> selectByAutoName(int i){
        return relevancyMapper.selectByAutoName(i);
    }

    /**
     * 根据店铺Id查询今日新增车辆名称
     * @param i
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Relevancy> selectByAutoNow(int i){
        return relevancyMapper.selectByAutoNow(i,new Date());
    }

    /**
     * 根据店铺id查询所有车辆信息
     * @param i
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Relevancy> selectByAuto(int i){
        return relevancyMapper.selectByAuto(i);
    }

    /**
     * @Description: 根据店铺id查询车辆信息并且对车辆信息模糊查询
     * @Author: zyx
     * @Date: 20:47 2022/2/14
     * @Name: queryAllScreen
     * @Param: [relevancy]
     * @Return: java.util.List<com.zyx.pojo.Relevancy>
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Relevancy> queryAllScreen(Integer pageNum, Integer pageSize, Relevancy relevancy){
        PageHelper.startPage(pageNum,pageSize);
        List<Relevancy> list = relevancyMapper.queryAllScreen(relevancy);
        return new PageInfo<>(list);
    }

    /**
     * 查询店铺某个车辆类型的车辆型号
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Relevancy> queryByAuto(Integer pageNum, Integer pageSize, Relevancy relevancy){
        PageHelper.startPage(pageNum,pageSize);
        List<Relevancy> list = relevancyMapper.queryByAuto(relevancy.getSpencerId(),relevancy.getCarId());
        return new PageInfo<>(list);
    }

    /**
     * @Description: 根据店铺id查询车辆信息并且对车辆信息模糊查询
     * @Author: zyx
     * @Date: 20:47 2022/2/14
     * @Name: queryAllScreen
     * @Param: [relevancy]
     * @Return: java.util.List<com.zyx.pojo.Relevancy>
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Relevancy> queryByType(Integer pageNum, Integer pageSize, Relevancy relevancy){
        PageHelper.startPage(pageNum,pageSize);
        List<Relevancy> list = relevancyMapper.queryByType(relevancy.getSpencerId());
        for (Relevancy r : list){
            Name n = r.getName();
            System.out.println(n.getCarName());
            n.setPriceMax(autoService.queryMax(n.getCarName()));
            n.setPriceMin(autoService.queryMin(n.getCarName()));
            r.setName(n);
        }
        return new PageInfo<>(list);
    }

    //根据车辆类型id删除店铺中的车辆信息
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer deleteByAutoId(Relevancy r){
        return relevancyMapper.deleteByAutoId(r);
    }

    //根据车辆id和店铺id查询是否含有值
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public List<Relevancy> exists(Relevancy r){
        return relevancyMapper.exists(r.getSpencerId(),r.getCarId(),r.getAutoId());
    }

    //根据车辆名称id删除店铺中的车辆信息
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer deleteByCarId(Relevancy r){
        return relevancyMapper.deleteByCarId( r.getCarId(),r.getSpencerId());
    }

    //查询所有店铺车辆和店铺名称
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Relevancy> queryAll(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Relevancy> list = relevancyMapper.queryAll();
        for (Relevancy r : list){
            Name n = r.getName();
            System.out.println(n.getCarName());
            n.setPriceMax(autoService.queryMax(n.getCarName()));
            n.setPriceMin(autoService.queryMin(n.getCarName()));
            r.setName(n);
        }
        return new PageInfo<>(list);
    }

    /**
     * @Description: 添加店铺车辆
     * @Author: zyx
     * @Date: 10:04 2022/2/19
     * @Name: addRelevancy
     * @Param: [relevancy]
     * @Return: int
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addRelevancy(Relevancy relevancy){
        return relevancyMapper.insertSelective(relevancy);
    }

    /**
     * @Description: 删除店铺车辆
     * @Author: zyx
     * @Date: 10:12 2022/2/19
     * @Name: deleteByExample
     * @Param: [spencerId, autoId]
     * @Return: int
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteByExample(int spencerId,int autoId){
        RelevancyExample example = new RelevancyExample();
        RelevancyExample.Criteria criteria = example.createCriteria();
        criteria.andAutoIdEqualTo(autoId);
        criteria.andSpencerIdEqualTo(spencerId);
        return relevancyMapper.deleteByExample(example);
    }
}
